#include "cmd_photo.h"
#include <iostream>

cmd_photo::cmd_photo(){
}

cmd_photo::cmd_photo(Photo * photo):photo(photo){
}

string cmd_photo::query(){
    stringstream ss;

    switch(cmd){
        case CHECKALBUM:
        case ADDPHOTOALBUM:
        case CHECKLIMITALBUM:
            if(album == 0)
                throw("Album invalido!");
            break;
        case CADASTRAR:
        case CHECKEXCLUIR:
        case EXCLUIR:
        case CHECKPHOTO:
        case PESQUISAR:
        default:
            if(photo == 0)
                throw("Foto invalida!");
            break;
    }
    switch(cmd){
        case CADASTRAR:
            ss << "INSERT INTO tb_photos VALUES("
                << "'" << photo->getIdentifier().get() << "',"
                << "'" << photo->getDate().get() << "',"
                << "" << photo->getSpeed().get() << ","
                << "" << photo->getOpenness().get() << ","
                << "" << photo->getISO().get() << ""
            << ");";
            break;
        case CHECKEXCLUIR:
            ss << "SELECT id_photo FROM tb_photosalbum as pa "
                << "INNER JOIN tb_photos as p "
                << "ON pa.id_photo = p.identifier WHERE "
                << "identifier = '" << photo->getIdentifier().get() << "'"
            << ";";
            break;
        case EXCLUIR:
            ss << "DELETE FROM tb_photos WHERE "
                << "identifier = '" << photo->getIdentifier().get() << "'"
            << ";";
            break;
        case CHECKPHOTO:
            ss << "SELECT identifier FROM tb_photos WHERE "
                << "identifier = '" << photo->getIdentifier().get() << "'" << ""
            << ";";
            break;
        case CHECKALBUM:
            ss << "SELECT identifier FROM tb_albuns WHERE "
                << "identifier = '" << album->getIdentifier().get() << "'"
                << " and cpf = " << "'cpf_usuario'" << ""
            << ";";
            break;
        case ADDPHOTOALBUM:
            ss << "INSERT INTO tb_photosalbum VALUES("
                << "'" << album->getIdentifier().get() << "',"
                << "'" << photo->getIdentifier().get() << "'"
            << ");";
            break;
        case CHECKLIMITALBUM:
            ss << "SELECT id_photo FROM tb_photosalbum WHERE "
                << "id_album = '" << album->getIdentifier().get() << "'"
            << ";";
            break;
        case PESQUISAR:
        default:
            ss << "SELECT * FROM tb_photos WHERE "
                << "identifier LIKE '%" << photo->getIdentifier().get() << "%'" << ""
            << ";";
            break;
    }
    return ss.str();
}

void cmd_photo::setPhoto(Photo * photo){
    this->photo = photo;
}

Photo * cmd_photo::getPhoto(){
    return photo;
}

void cmd_photo::setAlbum(Album * album){
    this->album = album;
}

Album * cmd_photo::getAlbum(){
    return album;
}

void cmd_photo::setCmd(int cmd){
    this->cmd = cmd;
}


cmd_album::cmd_album(){
}

cmd_album::cmd_album(Album * album):album(album){
}

string cmd_album::query(){
    stringstream ss;
    switch(cmd){
        case CHECKCADASTRAR:
            break;
        case PESQFOTOSALBUM:
        case CHECKALBUM:
        case CHECKEXCLUIR:
        case CADASTRAR:
        case EXCLUIR:
        case PESQUISAR:
        default:
            if(album == 0)
                throw("Album invalido!");
            break;
    }
    switch(cmd){
        case CHECKCADASTRAR:
            ss << "SELECT identifier FROM tb_albuns WHERE "
                << "cpf = 'cpf_usuario'"
            << ";";
            break;
        case CADASTRAR:
            ss << "INSERT INTO tb_albuns VALUES("
                << "'" << album->getIdentifier().get() << "',"
                << "'" << album->getDescription().get() << "',"
                << "" << album->getAccessLevel().get() << ","
                << "" << album->getPrice().get() << ","
                << "" << "'cpf_usuario'" << ""
            << ");";
            break;
        case EXCLUIR:
            ss << endl << "DELETE FROM tb_photosalbum WHERE "
                << "id_album = '" << album->getIdentifier().get() << "'"
            << ";";
            ss << "DELETE FROM tb_albuns WHERE "
                << "identifier = '" << album->getIdentifier().get() << "'"
            << ";";
            break;
        case CHECKEXCLUIR:
            ss << "SELECT s.identifier FROM tb_sales as s "
                << "INNER JOIN tb_albuns as a "
                << "    ON s.identifier = a.identifier "
                << "WHERE "
                << "a.identifier = '" << album->getIdentifier().get() << "'"
                << " and a.cpf = " << "'cpf_usuario'" << ""
            << ";";
            break;
        case PESQFOTOSALBUM:
            ss << "SELECT p.identifier FROM tb_photos as p "
                << "INNER JOIN tb_photosalbum as pa"
                << "    ON p.identifier = pa.id_photo "
                << "INNER JOIN tb_albuns as a"
                << "    ON pa.id_album = a.identifier "
                << "WHERE "
                << "a.identifier = '" << album->getIdentifier().get() << "'"
                << " and a.cpf = " << "'cpf_usuario'" << ""
            << ";";
            break;
        case CHECKALBUM:
            ss << "SELECT identifier FROM tb_albuns WHERE "
                << "identifier = '" << album->getIdentifier().get() << "'"
                << " and cpf = " << "'cpf_usuario'" << ""
            << ";";
            break;
        case PESQUISAR:
        default:
            ss << "SELECT * FROM tb_albuns WHERE "
                << "identifier LIKE '%" << album->getIdentifier().get() << "%'"
                << " and cpf = " << "'cpf_usuario'" << ""
                << " or access_level = 0"
            << ";";
            break;
    }
    return ss.str();
}

void cmd_album::setAlbum(Album * album){
    this->album = album;
}

Album * cmd_album::getAlbum(){
    return album;
}

void cmd_album::setCmd(int cmd){
    this->cmd = cmd;
}
